//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//
//	File		 :	HTMLFormattedOutput.h
//	Contents	 :	Source file for class CHTMLFormattedOutput
//	Creation Date:	22/12/1999
//	Last Modified:	23/12/1999	lg
//	tabs width	 :	every 4 characters

//
//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#if !defined(HTMLFORMATTEDOUTPUT_H)
#define HTMLFORMATTEDOUTPUT_H

#include "formattedoutput.h"


//_______________________________________________________________________________________
//
//	CHTMLFormattedOutput
//
//		class for producing formatted HTML
//
//		Generally, the only necessary function calls are (in order):
//
//			1) AddFormat/AddFormats:
//				adds all the text formats which will be used throughout
//
//			2) BeginHeader()
//
//			3) AddText, AddNewLine
//			   SetFormat
//				switches to one of the formats from AddFormat(s)
//
//			5) EndFooter()
//
//			6) 	CopytoClipboard() or
//				GetStreamedText() or
//				CopyToWord()	  or
//				AddToWordDocument()
//
//			
//		Implementation:
//			For performance reasons, text is added to a queue
//			 which is flushed only on format change, new line or at the end
//	
//_______________________________________________________________________________________


//_______________________________________________________________________________________
//
//	CHTMLFormattedOutput
//
//		produces formatted text in the Microsoft Rich Text Format
//_______________________________________________________________________________________
class CHTMLFormattedOutput : virtual public CFormattedOutput
{
// data
							// style names use this string with numerals appended
	std::string				StyleRoot;
							// text for style tags
	std::vector<std::string> VecStylesText;
							// whether an embedded CSS (true) or inline styles used(false)
	bool					UseStyleSheet;
							// whether use <SAMPLE> tag
	bool					IgnoreFont;
	bool					IgnoreFontSize;
	std::string				HTMLFormattedStream;
	std::string				HTMLClipStream;
	
	void					StreamText(char* s);
	void					StreamText(char s);
	void					StreamText(const std::string& s);
	void					CreateStylesText();
	std::string				Format_to_StyleText(const CFormattedOutput::CTextFormat& f);
protected:
	const std::string&		GetHTMLFormattedStream() const;
	const std::string&		GetHTMLClipStream() const;
	void					SetHTMLClipStream(const std::string& s);
	void					CreateStyleSheet();
	void					CreateStyleSheetData();
	void					ClearStream();
	void					StreamQueuedText();
	virtual void			DoBeginHeader();
	virtual void			DoEndFooter();
#ifdef _MFC_VER	// mfc specific code to follow
	unsigned					GetClipboardFormat(unsigned i);
#endif


public:
	const std::string&		GetStyleRoot() const {return StyleRoot;}
							CHTMLFormattedOutput(unsigned ts = 100,
								const std::string& tf = "Courier New",
								bool useStyleSheet = false,
								const std::string& stylesRoot = "lg",
								bool ignoreFont = true,
								bool ignoreFontSize = true):
								CFormattedOutput(ts, tf),
								StyleRoot(stylesRoot),
								UseStyleSheet(useStyleSheet),
								IgnoreFont(ignoreFont),
								IgnoreFontSize(ignoreFontSize)
								{}
	unsigned					GetFileFormatCounts();
#ifdef _MFC_VER	// mfc specific code to follow
	const std::string&		GetClipStreamedText(unsigned i);
#endif

	const std::string&		GetStreamedText(unsigned i);
	void					AddNewLine();
	void					AddNewLineAndHalf();
	void					AddDoubleNewLines();
};


//_______________________________________________________________________________________
//
//	CHTMLFormattedOutput
//
//		just produces the style sheet
//_______________________________________________________________________________________
class CHTMLStylesFormattedOutput : virtual public CHTMLFormattedOutput
{
#ifdef _MFC_VER	// mfc specific code to follow
	unsigned			GetClipboardFormat(unsigned i);
#endif
public:   				CHTMLStylesFormattedOutput(unsigned ts = 100,
													const std::string& tf = "Courier New",
													const std::string& stylesRoot = "lg",
													bool ignoreFont = true) :
						CHTMLFormattedOutput(ts, tf, true, stylesRoot, ignoreFont){}
	unsigned			GetFileFormatCounts();
#ifdef _MFC_VER	// mfc specific code to follow
	const std::string&	GetClipStreamedText(unsigned i);
#endif
protected:
	virtual void		DoBeginHeader();
	virtual void		DoEndFooter();
};


//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//
//	CHTMLFormattedOutput
//
//		inlines mainly for encapsulation
//
//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
inline const std::string& CHTMLFormattedOutput::GetHTMLFormattedStream() const
{
	return HTMLFormattedStream;
}

inline const std::string& CHTMLFormattedOutput::GetHTMLClipStream() const
{
	return HTMLFormattedStream;
}

inline void CHTMLFormattedOutput::SetHTMLClipStream(const std::string& s)
{
	HTMLClipStream = s;
}


inline void CHTMLFormattedOutput::StreamText(char* s)
{
	HTMLFormattedStream += s;
}

inline void CHTMLFormattedOutput::StreamText(char s)
{
	HTMLFormattedStream += s;
}

inline void CHTMLFormattedOutput::StreamText(const std::string& s)
{
	if (HTMLFormattedStream.length() + s.length() > HTMLFormattedStream.capacity())
		HTMLFormattedStream.reserve(HTMLFormattedStream.length() * 2 + 1);
	HTMLFormattedStream.append(s);
}

//_______________________________________________________________________________________
//
//	ClearStream
//
//		Reinitialize Stream, should be called by BeginHeader()
//		Last modified: lg 23/12/1999
//		
//_______________________________________________________________________________________
inline void CHTMLFormattedOutput::ClearStream()
{
	HTMLFormattedStream.erase();
}


#endif // !defined(HTMLFORMATTEDOUTPUT_H)
